package com.wl.api.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wl.api.common.utils.DateUtil;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;

@RestController
@RequestMapping("/common")
public class UploadController extends CommonController {
	//@Value("${WBSystem.LOGINURL}")
	@Value("${file_path}")
	private String static_path;
	
	@RequestMapping(value="/test")
	public void testMethod () {
		System.out.println(static_path);
		System.out.println(static_path);
	}
	
	@RequestMapping(value ="/upload")
	public JsonObject uploadFile(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
		JsonObject json = JsonObject.getInstance();
		if (!file.isEmpty()) {
			String datefile = DateUtil.format(new Date(), DateUtil.YYYYMMDD);
			String path = static_path + datefile;
			
			String fileName = StringUtil.getRandomDateID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			System.out.println(fileName);
			File filePath = new File(path, fileName);
			if (!filePath.exists()) { 
				filePath.mkdirs();
            }
			file.transferTo(filePath);
			json.setData("/"+datefile+"/"+fileName);
		}
		setJsonMessage(json);
		return json;
	}
}
